package com.china08.yxyapi.api.manage.edu.office.innernotice;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner.YxyNotifictionTea;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

@CrossOrigin
@RestController
public class InnerNoticeReceiveNoReadCountControlelr extends YxyApiBaseController {

    @Autowired
    YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/office/innerNotice/receive/noread/count", method = RequestMethod.POST)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        YxyProfile yxyProfile = getProfile(request, response);
        String schoolId = yxyProfile.getSchoolId();
        String userId = yxyProfile.getId();
        Integer count = 0;
        // 当前用户所有通知
        List<YxyNoticeInner> list = yxyNoticeInnerRepository.findBySchoolIdAndReceiverUserIdAndReceiverIsDel(schoolId, userId, 0);
        for (YxyNoticeInner inner : list) {
            // 所有接受人
            List<YxyNotifictionTea> receiveList = inner.getReceiver();
            for (YxyNotifictionTea tea : receiveList) {
                // 接受人的id
                String receiveId = tea.getUserId();
                // 是否已读
                Integer isRead = tea.getIsRead();
                // 发给自己的
                if (receiveId.equals(userId) && isRead == 0) {
                    // 未读
                    count = count + 1;
                }
            }
        }
        RespModel respModel = new RespModel();
        respModel.noReadCount = count;
        return respModel;
    }

    public static class ReqModel extends YxyApiPageReqModel {

    }

    public static class RespModel {
        public Integer noReadCount;
    }
}
